package com.lantosec.hmrp.modules.system.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.util.ExcelUtils;
import com.lantosec.hmrp.modules.system.vo.ReportExecutionVO;
import com.lantosec.hmrp.modules.system.vo.SysUserVO;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.system.entity.SysCustomReport;
import com.lantosec.hmrp.modules.system.service.ISysCustomReportService;
import com.lantosec.hmrp.modules.system.vo.SysCustomReportVO;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

import javax.servlet.http.HttpServletResponse;

/**
 *
 * 自定义报表查询表 控制器
 *
 * @author AutoCode
 * @since 2020-06-14
 */
@Api(value = "自定义报表查询表控制器", tags = "自定义报表查询表控制器")
@RestController
@RequestMapping("/system/report/")
@Log4j2
public class SysCustomReportController extends BaseController {
    @Autowired
    private ISysCustomReportService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SysCustomReportVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<SysCustomReportVO>> page(@ApiParam(value = "分页对象", required = true) Page<SysCustomReport> page, SysCustomReportVO condition){
        page = service.page(page,
                new QueryWrapper<SysCustomReport>()
                        .like(StringUtils.isNotEmpty(condition.getReportName()),String.valueOf(SysCustomReport.ColName.report_name),condition.getReportName())
                        //.eq(Objects.isNull(condition.getEnable()),String.valueOf(SysCustomReport.ColName.enable),condition.getEnable())
        );
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SysCustomReportVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "自定义报表新增数据", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "自定义报表查询表VO对象", required = true) @RequestBody @Validated SysCustomReportVO vo){
        SysCustomReport entity = vo.convertToEntity();
        service.save(entity);
        return Result.ok(entity.getId());
    }

    @PutMapping("update")
    @ControllerLog(name = "自定义报表修改数据", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "自定义报表查询表VO对象", required = true) @RequestBody SysCustomReportVO vo){
        SysCustomReport entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "自定义报表逻辑删除", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("executionQuery")
    @ApiOperation(value = "执行报表查询")
    public Result<Page<Map<String, Object>>> executionQuery(@ApiParam(value = "分页对象", required = true) PageVO<Map<String,Object>> page
                                                            ,@ApiParam(value = "查询参数", required = true) ReportExecutionVO executionVO){
        try{
            return Result.ok(service.executionQuery(page, executionVO));}
        catch (Exception ex){
            return Result.fail(ex.getMessage());
        }
    }

    @GetMapping("export")
    @ApiOperation("导出数据")
    public void export(@ApiParam(value = "查询参数", required = true) ReportExecutionVO executionVO, HttpServletResponse response) {
        service.exportExcel(executionVO, response);
    }
}
